package com.buss.admin.controller;

import com.buss.admin.entity.AdminUserEntity;
import com.buss.admin.entity.UserRoleEntity;
import com.buss.admin.service.AdminUserServiceI;
import com.buss.admin.trans.form.AdminUserForm;
import com.buss.admin.trans.form.MemberForm;
import com.buss.auth.transfer.vo.authen.AuthUserVOAdmin;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.service.SysServiceI;
import com.buss.common.util.IDUtils;
import com.buss.member.entity.GeneralUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/members")
public class AdminUserController {

	@Autowired
	private AdminUserServiceI adminUserServiceI;

	@Autowired
	private SysServiceI sysServiceI;

	/*
	 * 根据管理员的身份 查询所有会员列表
	 */
	@RequestMapping("/chckelistss")
	@ResponseBody
	public EasyUIDataGridResult geetCheckMemberListss(AdminUserForm adminUserForm){
	// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();


				CriteriaQuery cq1 = adminUserForm.getCriteriaQuery();

				List<AdminUserEntity> pageMode = this.sysServiceI.list(cq1);

				List<GeneralUserEntity> userEntities = new ArrayList<>();
				for (AdminUserEntity au : pageMode) {
					GeneralUserEntity userEntity = new GeneralUserEntity();
					MyBeanUtils.copyBeanNotNull2Bean(au.getUserEntity(), userEntity);

					userEntities.add(userEntity);
				}

				// 封装数据
				result.setRows(userEntities);






		return result;

	}


	/*
	 * 根据管理员的身份 查询所有会员列表并分页
	 */
	@RequestMapping("/chckelists")
	@ResponseBody
	public EasyUIDataGridResult geetCheckMemberList(AdminUserForm adminUserForm){
// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		// 获得后台用户信息
		Subject currentUser = SecurityUtils.getSubject();
		AuthUserVOAdmin user = (AuthUserVOAdmin) currentUser.getSession().getAttribute("userInfoAdmin");
		String uid = user.getId();

		List<UserRoleEntity> roleList = this.sysServiceI.findByProperty(UserRoleEntity.class, "user.id", uid);


		if (roleList!=null && roleList.size()>0) {
			int pageNo = Integer.parseInt(adminUserForm.getPage());
			int pageSize = Integer.parseInt(adminUserForm.getRows());
			if (!roleList.get(0).getRole().getCode().equals("admin")) {
				adminUserForm.setAid(uid);
				CriteriaQuery cq1 = adminUserForm.getCriteriaQuery();




				PageMode<AdminUserEntity> pageMode = adminUserServiceI.list4page(cq1, pageNo, pageSize);

				List<GeneralUserEntity> userEntities = new ArrayList<>();
				for (AdminUserEntity au : pageMode.getResult()) {
					GeneralUserEntity userEntity = new GeneralUserEntity();
					MyBeanUtils.copyBeanNotNull2Bean(au.getUserEntity(), userEntity);

					userEntities.add(userEntity);
				}

				// 封装数据
				result.setRows(userEntities);
				result.setTotal(pageMode.getTotal());
			} else {
				MemberForm memberForm = new MemberForm();
				CriteriaQuery cq = memberForm.getCriteriaQuery();
				PageMode<GeneralUserEntity> pageMode = sysServiceI.list4page(cq, pageNo, pageSize);
				result.setRows(pageMode.getResult());
				result.setTotal(pageMode.getTotal());
			}

		} else  {
			result.setRows(null);
			System.out.print("该用户未授权");
		}




		return result;

	}

	@RequestMapping("/addToAdmin")
	@ResponseBody
	public HXResult deleteUser(String adminid , String ids) {

		// 删除以前的关联
		this.sysServiceI.executeSql("DELETE FROM hx_admin_user WHERE aid = ?", adminid);

		// 添加新关联
		Date creteTime = new Date();
		Date updateTime = new Date();
		String sql = "INSERT INTO `hxclub`.`hx_admin_user` (`id`, `aid`, `uid`, `creteTime`, `updateTime`, `status`, `is_delete`)" +
													" VALUES (?, ?, ?, ?, ?, ?, ?);";
		try {

			String[] split = ids.split(",");
			for (String id : split) {
				// 添加关联关系
				String pk = IDUtils.getUUID();
				// MessageFormat.format(sql,adminid, id, creteTime, updateTime, 1, 0);
				this.sysServiceI.executeSql(sql, pk,adminid, id, creteTime, updateTime, 1, 0);
			}
			return HXResult.ok("添加数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，请稍后再试！");

		}
	}

	/*
	 * 管理员相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		return "admin/member/" + path;
	}

}
